package com.java.exer_5;

import javax.swing.*;
import java.util.Arrays;

public class RunGame {
    private static Player mine;
    private static Player computer;
    private static Poker[] pokers;
    // ��ǰ���Է��Ƶ�λ��
    private static int pokersIndex = 0;
    // �����Ҫ�Ƶ�����
    private static int LIMIT_POINTER = 18;

    public static void main(String[] args) {
        // ��ʼ����Ϸ
        initGame();
        // ��ʾ����
        viewDesktop();
    }

    /**
     * viewDesktop
     */
    public static void viewDesktop() {
        boolean flag = true;
        while (flag) {
            String info = "----------- 21����Ϸ -----------\n" +
                    computer + "\n" + mine.getInfo() + "\n" + "1- Ҫ��\t 2-ͣ��";

            String input = JOptionPane.showInputDialog(info);
            if (input == null || input.trim().isEmpty()) {
                JOptionPane.showMessageDialog(null, "����ȷ����");
                continue;
            }
            switch (input.trim().charAt(0)) {
                case '1':
                    //�Լ�Ҫ��
                    mine.getPoker(pokers[pokersIndex++]);
                    //����Ҫ��
                    if (computer.getPointers() < LIMIT_POINTER) {
                        computer.getPoker(pokers[pokersIndex++]);
                    }
                    break;
                case '2':
                    //�����ж��Ƿ���Ҫ��
                    while (computer.getPointers() < LIMIT_POINTER) {
                        computer.getPoker(pokers[pokersIndex++]);
                    }
                    //�ж���Ӯ
                    boolean isWin = true;
                    if (computer.getPointers() == 21) {
                        //�ж�˭�� black jack ˭Ӯ��������� black jack �����Ӯ��

                        isWin = false;
                    } else if (mine.getPointers() > 21) {
                        isWin = false;
                    } else if (computer.getPointers() < 21) {
                        isWin = mine.getPointers() > computer.getPointers();
                    }
                    //�Ƚϵ�������ʾ���
                    info = "----------- ��" + (isWin ? "Ӯ��" : "����") + " -----------\n" +
                            computer.getInfo() + "  :" + computer.getPointers() + "\n" +
                            mine.getInfo() + "  :" + mine.getPointers();
                    JOptionPane.showMessageDialog(null, info);
                    //������Ϸ
                    flag = false;
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "����ȷ����");
                    continue;
            }
        }
    }

    /**
     * ��ʼ����Ϸ
     */
    private static void initGame() {
        // ����һ���˿�
        initPokers();

        // �����������
        String playerName = JOptionPane.showInputDialog("������������֣�");
        if (playerName == null || playerName.trim().isEmpty()) {
            playerName = "����";
        }
        mine = new Player(playerName);
        // �����������
        computer = new Player("�߽�");

        // ��ʼ����ÿ�˷�������
        computer.getPoker(pokers[pokersIndex++]);
        mine.getPoker(pokers[pokersIndex++]);
        computer.getPoker(pokers[pokersIndex++]);
        mine.getPoker(pokers[pokersIndex++]);
    }

    /**
     * ����һ���˿ˣ���ϴ��
     */
    private static void initPokers() {
        pokers = new Poker[52];

        for (int i = 0; i < 4; i++) {
            for (int j = 1; j < 14; j++) {
                int index = i * 13 + j - 1;
                pokers[index] = new Poker(i, j);

            }
        }

        Arrays.sort(pokers);

    }


}
